<?php
namespace Home\Controller;
use Home\Common\Common;
class StaffinfoController extends Common {

    public function index(){
        $staff= M('staffinfo');
        if (empty($_GET['search']) || empty($_GET['value'])){
            $where = array('statuss'=>0);
        }else{
            $where =array('`stf_name` like "%'.$_GET['value'].'%" or `stf_email` like "%'.$_GET['value'].'%"','statuss'=>0)  ;
            $this->assign('value',$_GET['value']);
        }

        $count = $staff->where($where)->join('postinfo on staffinfo.pt_id=postinfo.pt_id')->join('departmentinfo on staffinfo.dpt_id=departmentinfo.dpt_id')->count();

        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $staff->where($where)->join('postinfo on staffinfo.pt_id=postinfo.pt_id')->join('departmentinfo on staffinfo.dpt_id=departmentinfo.dpt_id')->limit($page->firstRow.','.$page->listRows)->order('stf_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();
    }
    public function hidden(){
        $staff= M('staffinfo');
        if (empty($_GET['search']) || empty($_GET['value'])){
            $where = array('statuss'=>1);
        }else{
            $where =array('`stf_name` like "%'.$_GET['value'].'%" or `stf_email` like "%'.$_GET['value'].'%"','statuss'=>1)  ;
            $this->assign('value',$_GET['value']);
        }
        $count = $staff->where($where)->join('postinfo on staffinfo.pt_id=postinfo.pt_id')->join('departmentinfo on staffinfo.dpt_id=departmentinfo.dpt_id')->count();
        $page = new \Think\Page($count,10);
        $show = $page->show();
        $list = $staff->where($where)->join('postinfo on staffinfo.pt_id=postinfo.pt_id')->join('departmentinfo on staffinfo.dpt_id=departmentinfo.dpt_id')->limit($page->firstRow.','.$page->listRows)->order('stf_id')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('list',$list);
        $this->display();
    }
    public function add(){

        if (empty($_POST['submit'])){
            $staff = M('staffinfo');
            $post=M('postinfo');
            $department=M('departmentinfo');
            $post=$post->select();
            $department=$department->select();
            $staffList = $staff->order('stf_id')->select();
            $this->assign('staffList',$staffList);
            $this->assign('post',$post);
            $this->assign('department',$department);
            $this->display();
        }else{
            $staff = D('staffinfo');

            $idcard=$staff->where('`stf_bank`="'.$_POST['stf_bank'].'"')->select();
            $sname=$staff->where('`stf_name`="'.$_POST['stf_name'].'"')->select();
            $semail=$staff->where('`stf_email`="'.$_POST['stf_email'].'"')->select();
            $phone=$staff->where('`stf_phone`="'.$_POST['stf_phone'].'"')->select();
            if(!empty($idcard)){
               $this->error('该身份证已存在！',U('add'));
            }else{
                $length=' /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/';
                if(!preg_match($length,$_POST['stf_bank'])){
                    $this->error('身份证格式不正确！',U('add'));
                }
                $sfz1=substr($_POST['stf_bank'],6,4);
                $now=date('Y-m-d',time());
                $sss = explode("-", $now);
                if(intval($sss[0])-intval($sfz1)<=18){
                    $this->error('员工未满18岁！',U('add'));
                }
            }
            if(!empty($sname)){
               $this->error('该用户名已存在！',U('add'));
            }
            if(!empty($semail)){
                $this->error('该邮箱已被使用！',U('add'));
            }
            if(!empty($phone)){
                $this->error('该手机号已被使用！',U('add'));
            }

            if(empty($_POST['stf_name'])||empty($_POST['stf_brith'])||empty($_POST['wd_time'])||empty($_POST['stf_email'])||empty($_POST['stf_study'])||empty($_POST['stf_phone'])||empty($_POST['stf_bank'])){
                $this->error('请填写关键数据！',U('add'));
            }else{
                $phone='#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#';
                if(!preg_match($phone,$_POST['stf_phone'])){
                    $this->error('联系方式不正确！',U('add'));
                }else{
                        if(!empty($_POST['rd_time'])){
                            $_POST['statuss']=1;
                        }
                        $_POST['stf_pwd']=md5(123456);
                        $staff->add($_POST);
                        $this->success('添加成功!',U('index'));
                }
            }
        }
    }

    public function api(){
        $post=M('postinfo');
        $post=$post->where('dpt_id='.$_GET['dpt_id'])->select();
        $this->ajaxReturn($post);
    }

    public function del(){
        if (empty($_GET['stf_id'])){
            $this->error('参数错误！',U('index'));
        }else{
            $product = M('staffinfo');
            $_GET['statuss']=1;
            $product->save($_GET);
            $this->success('隐藏成功！',U('index'));
        }

    }

    public function del_all(){
        $admin = M('staffinfo');
        if(empty($_GET['ids'])){
            $this->error('未选择隐藏项！',U('index'));
        }else {
            $str =  implode(',', $_GET['ids']);
            $arr = explode(",", $str);
            foreach ($arr as $a) {
                $admin->where(array('stf_id'=>$a))->save(array('statuss'=>1));
            }
            $this->success('隐藏成功！', U('index'));
        }
    }


    public function edit(){
        if (empty($_POST['submit'])){
            if (empty($_GET['stf_id'])) {
                $this->error('参数错误!', U('index'));
            }
            $post=M('postinfo');
            $department=M('departmentinfo');
            $post=$post->select();
            $department=$department->select();
            $admin = M('staffinfo');
            $info = $admin->where('`stf_id`='.$_GET['stf_id'])->find();
            $this->assign('info',$info);
            $this->assign('post',$post);
            $this->assign('department',$department);
            $this->display();
        }else{
            if($_POST['stf_pwd']!=$_POST['pwd2']){
                $this->error('修改失败！两次密码不一致！',U('edit',array('stf_id'=>$_POST['stf_id'])));
            }else{
                if(!empty($_POST['rd_time'])){
                    $_POST['statuss']=1;
                }
                $_POST['stf_pwd']=md5($_POST['stf_pwd']);
                $staff = D('staffinfo');
                $staff->where('`stf_id`='.$_POST['stf_id'])->save($_POST);
                $this->success('修改成功!',U('index'));
            }

        }

    }

    public function update()
    {
        if (empty($_POST['submit'])) {
            if (empty($_GET['stf_id'])) {
                $this->error('参数错误');
            }
            //查询前公司、部门、岗位
            $staff = M('staffinfo')->join('`departmentinfo` on `departmentinfo`.dpt_id=`staffinfo`.dpt_id')->join('`compnay` on `departmentinfo`.cmpy_id=`compnay`.cmpy_id')->join('`postinfo` on `postinfo`.pt_id=`staffinfo`.pt_id')->where('`staffinfo`.stf_id=' . $_GET['stf_id'])->find();
            $this->assign('staff', $staff);
            $company = M('compnay');//公司表
            $companyList = $company->select();
            $this->assign('company', $companyList);
            $this->display();
        } else {
            $company = M('recordinfo');//公司表
            $_POST['rmd_time'] = date('Y-m-d H:i');


            $_POST['status']=1;

            $setinfo = M('setinfo'); //流程设置表
            $atrtypeinfo=M('atrtypeinfo');
            $at_type=$atrtypeinfo->where(array('atrnmae'=>'调遣'))->find();
            $_POST['at_type']=$at_type['atrid'];

            $asCount = $setinfo->where('`artid`='.$at_type['atrid'].' and `states`=1')->count();
            $levelInfo = $setinfo->where('`artid`='.$at_type['atrid'].' and `pt_id`='.$this->_listprid['pt_id'])->find();

            if (empty($levelInfo)){
                $_POST['status'] = 1;
            }else{
                if ($levelInfo['sort'] == $asCount){
                    $_POST['status'] = 0;
                    $this->success('调遣成功',U('index'));
                }else{
                    $_POST['status'] = $levelInfo['sort'] + 1;
                }
            }

            $company->add($_POST);
//            exit(var_dump($company->getLastSql()));
            $this->success('提交成功!',U('index'));
        }
    }
    //查询部门
    public function bm(){
        $dpt = M('departmentinfo');
        $data = $dpt->where('`cmpy_id`='.$_GET['gs_id'])->select();
        $this->ajaxReturn($data,'JSON');
    }
    //查询岗位
    public function gw(){
        $pt = M('postinfo');
        $data = $pt->where('`dpt_id`='.$_GET['gw_id'])->select();
        $this->ajaxReturn($data,'JSON');
    }

    public function open(){
        if (empty($_GET['stf_id'])){
            $this->error('参数错误！',U('index'));
        }else{
            $product = M('staffinfo');
            $_GET['statuss']=0;
            $product->save($_GET);  //删除菜单
            $this->success('还原成功！',U('hidden'));
        }
    }

    public function open_all(){
        $admin = M('staffinfo');
        if(empty($_GET['ids'])){
            $this->error('未选择还原项！',U('index'));
        }else {
            $str =  implode(',', $_GET['ids']);
            $arr = explode(",", $str);
            foreach ($arr as $a) {
                $admin->where(array('stf_id'=>$a))->save(array('statuss'=>0));
            }
            $this->success('还原成功！', U('hidden'));
        }
    }

    public function repwd(){
        $staff = D('staffinfo');
        $staff->where('`stf_id`='.$_GET['stf_id'])->save(array('stf_pwd'=>md5(123456)));
        $this->success('重置成功!',U('index'));
    }
}